package cst.neu.yuejian_31.fragment;


import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.astuetz.PagerSlidingTabStrip;
import com.jude.rollviewpager.RollPagerView;
import com.jude.rollviewpager.adapter.StaticPagerAdapter;

import cst.neu.yuejian_31.R;

/**
 * A simple {@link Fragment} subclass.
 */
public class ActivityFragment extends BaseFragment {
    private View view=null;
    private PagerSlidingTabStrip pagersliding;
    private ViewPager viewpager;
    private EventFragment EventFragment=null;
    private MasterFragment MasterFragment=null;
    private MatchFragment MatchFragment=null;
    private DisplayMetrics dm;
    private RollPagerView mRollViewPager;

    public ActivityFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view= inflater.inflate(R.layout.fragment_club, container, false);
        init1();
        return view;

    }

    private void init1() {
        dm = getResources().getDisplayMetrics();//获取屏幕密度
        viewpager=(ViewPager)view.findViewById(R.id.viewPager);
        viewpager.setAdapter(new ActivityFragment.MyPagerAdapter(getChildFragmentManager()));
        pagersliding=(PagerSlidingTabStrip) view.findViewById(R.id.pagerslidingtabstrip);
        pagersliding.setViewPager(viewpager);
        setpagerstyle();//设置PagerSlidingTabStrip显示效果
    }
    private void setpagerstyle() {
        pagersliding.setShouldExpand(true); // 设置Tab是自动填充满屏幕的
        pagersliding.setDividerColor(Color.TRANSPARENT); // 设置Tab的分割线是透明的
        // 设置Tab底部线的高度
        pagersliding.setUnderlineHeight((int) TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP, 1, dm));
        // 设置Tab Indicator的高度
        pagersliding.setIndicatorHeight((int) TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP, 3, dm));
        // 设置Tab标题文字的大小
        pagersliding.setTextSize((int) TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_SP,16, dm));
        pagersliding.setIndicatorColor(Color.parseColor("#7FB12E"));// 设置Tab Indicator的颜色
        // 设置选中Tab文字的颜色 (这是自定义的一个方法)
//        pagersliding.setSelectedTextColor(Color.parseColor("#7FB12E"));
        pagersliding.setTabBackground(0); // 取消点击Tab时的背景色
    }
    private class MyPagerAdapter extends FragmentPagerAdapter {
        private String[] titles={"精品活动","权威大师","最近比赛"};//显示在二级导航上的标题文字
        public MyPagerAdapter(FragmentManager fm)    {  super(fm);    }
        @Override
        public CharSequence getPageTitle(int position) {return titles[position]; }
        public int getCount()
        {
            return titles.length;
        }

        @Override
        public Fragment getItem(int position) { //根据位置返回具体某个导航上对应的Fragment
            switch (position)
            {   case 0:
                if(EventFragment==null)  {  EventFragment=new EventFragment();}
                return EventFragment;
                case 1: if(MasterFragment==null) {  MasterFragment=new MasterFragment();  }
                    return MasterFragment;
                case 2: if(MatchFragment==null) {  MatchFragment=new MatchFragment();  }
                    return MatchFragment;
                default: return null;

            }
        }
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mRollViewPager = (RollPagerView) view.findViewById(R.id.roll_view_pager);
        mRollViewPager.setPlayDelay(1000);
        mRollViewPager.setAnimationDurtion(500);
        mRollViewPager.setAdapter(new ActivityFragment.TestNormalAdapter());
    }

    private class TestNormalAdapter extends StaticPagerAdapter {
        private int[] imgs = {
                R.drawable.julebu,
                R.drawable.basketball,
                R.drawable.run,
                R.drawable.go,
        };

        @Override
        public View getView(ViewGroup container, int position) {
            ImageView view = new ImageView(container.getContext());
            view.setImageResource(imgs[position]);
            view.setScaleType(ImageView.ScaleType.CENTER_CROP);
            view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            return view;
        }

        @Override
        public int getCount() {
            return imgs.length;
        }

    }
}
